﻿// JScript File

// Traffic Tracking
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-21857528-1']);
_gaq.push(['_trackPageview']);
(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


////////////////////////internal funciton////////////////////////
// these are Propotypes of String, not part of jQuery.
String.prototype.trim = function() {
	return replace(/^\s+|\s+$/,"");
}
String.prototype.ltrim = function() {
	return replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return replace(/\s+$/,"");
}

function trim(str)
{
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

var url = 'http://www.yulancan.com/securityws.asmx';
//var url = 'http://localhost:2674/WebSite/securityws.asmx';

var hintComment = '请分享您的评论!';
var hintSearch = '请输品名';
var curCommentAction = null;
function Comment(scoreId, level, content)
{
    var pl = new SOAPClientParameters();
	pl.add("scoreId", scoreId);
	pl.add("level", level);
	pl.add("content", content);
    SOAPClient.invoke(url, "Comment", pl, true, Comment_callBack);
}

function Comment_callBack(res)
{
    if (true == res)
    {     
        // what level has been submitted
        var $checkedRadio = $(curCommentAction).find('input:radio:checked');
        var $inputText = $(curCommentAction).find('input.comment_text');
        var submittedLevel = $checkedRadio.val();
        
        // update the level correspondingly.
        var $commentPanel = $(curCommentAction).closest('div.comment');
        var $thespan;
        switch (submittedLevel)
        {
        case '1':$thespan = $commentPanel.find('span.good_span');
        break;
        case '2':$thespan = $commentPanel.find('span.middle_span');
        break;
        case '3':$thespan = $commentPanel.find('span.bad_span');
        break;
        default:
        break;
        }

        $thespan.text(parseInt($thespan.text())+1);
        $(curCommentAction).fadeOut('slow');
        $checkedRadio.attr('checked', false);
        $inputText.val(hintComment);
    }   
}
function SubmitComment(commentAction)
{
    // precondition check.
    if (curCommentAction != commentAction)
    {
        // hide the previous comment div
        if (curCommentAction)
        {
            $(curCommentAction).fadeOut('slow');
        }   
        
        // show current comment div
        $(commentAction).fadeIn('slow');
        curCommentAction = commentAction;
        return false;
    }

    // submit the comment
    var level = $(curCommentAction).children('input:radio:checked').val();
    var content = $.trim($(curCommentAction).children('input.comment_text').val());
    if (content == hintComment && level == undefined)
    {
        $(curCommentAction).toggle('slow');
        return false;
    }
    
    if (level == undefined)
    {
        alert('请选择评价级别!');
        return false;
    }

    var href = $(commentAction).parents('div.comment').children('a').attr('href');
    var match = href.match(/score_id=(\d+)/i);
    var scoreId = match[1];
    
    Comment(scoreId, level, content);
    
    $(curCommentAction).fadeOut('slow');
}
function GetScore(scoreId)
{
    var pl = new SOAPClientParameters();
	pl.add("scoreId", scoreId);
    SOAPClient.invoke(url, "GetScore", pl, true, GetScore_callBack);
}
function GetScore_callBack(res)
{
    if (res != null)
    {
        var $commentPanel = $('div.comment');
        $commentPanel.find('span.good_span').text(res.Good);
        $commentPanel.find('span.middle_span').text(res.Middle);
        $commentPanel.find('span.bad_span').text(res.Bad);
    }
}
$(document).ready(function(){
    // Search/////////////////////////////////////////////////////////////////////
    $("#search_textbox").Watermark(hintSearch);
    $('#search_button').click(function(){
        var keywords = $.trim($("#search_textbox").val());
        if (keywords.length > 0)
        {
            window.location.href='/Public/searchresults-keywords='+keywords+'.aspx';
        }
    });
    $('#search_textbox').keypress(function(event){
        if (event.which == 13)
        {
            $('#search_button').click();
            return false;
        }
    });
    
   //Comment/////////////////////////////////////////////////////////////////////
   $("input:text.comment_text").Watermark(hintComment);
   
   // hide all commen panel at first.
//   $('div.comment_action').hide();
    
    $('input:button.comment').click(function(){
        var commentAction = $(this).next('div.comment_action').get(0);
        
        var sss = $('#comment_action_restaurant');
      
        SubmitComment(commentAction);
        
        return false;
    });
    
    // if enter pressed
    $('div.comment_action').keypress(function(event){
    if (event.which == 13)
    {
        SubmitComment(this);

        return false;
    }
    });
    
    // load comments for public/restaurant
    var commentActionRestruant = $('#comment_action_restaurant').get(0);
    if (undefined != commentActionRestruant)
    {
        var href = $(commentActionRestruant).parents('div.comment').children('a').attr('href');
        var match = href.match(/score_id=(\d+)/i);
        var scoreId = match[1];
        GetScore(scoreId);
    }
});